package cn.fanqinghong.org.controller;

import cn.fanqinghong.basic.util.AjaxResult;
import cn.fanqinghong.basic.query.PageList;
import cn.fanqinghong.org.domain.Employee;
import cn.fanqinghong.org.query.EmployeeQuery;
import cn.fanqinghong.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * FAN
 * 2022/6/5  10:57
 */
@RestController
@RequestMapping("/employee")
@Api(value = "部门的API" ,description = "部门的CRUD")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条数据")
    public Employee queyrById(@PathVariable("id") Long id){
       return employeeService.queryById(id);
    }

    /**
     * 查询全部
     * http://localhost:8080/dept
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部")
    public List<Employee> queryAll(){
        return employeeService.queryAll();
    }

    /**
     * 新增或者修改
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "新增或者修改")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId() == null){
                employeeService.save(employee);
                return AjaxResult.me();
            }
                employeeService.update(employee);
            return AjaxResult.me();
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("噢哟，系统瓦了，我们正在殴打程序员！！");
        }
    }
    /**
     * 批量删除数据
     * 前端传递的是数组，后端可以使用list或者数组接收
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult batch(@RequestBody List<Long> ids){
        try {
            employeeService.batch(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("抱歉出错了，我们正在联系李荣浩殴打艾正伟程序员！！！");
        }
    }

    /**
     * 删除一条数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一条数据")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            employeeService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("抱歉出错了，我们正在联系李荣浩殴打艾正伟程序员！！！");
        }
    }

    /**
     * 分页查询+高级查询
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "高级查询何分页查询")
    public PageList<Employee> queryAll(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);
    }


    /**
     * 查询一条数据
     * @param shopId
     * @return
     */
    @GetMapping("/list/{shopId}")
    @ApiOperation(value = "查询一条数据")
    public List<Employee> queyrByShopId(@PathVariable("shopId") Long shopId){
        return employeeService.queryByShopEmplyess(shopId);
    }
}
